package com.agv.core.lectorDeGlobalizadoresImpl;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashSet;

public class LectorDeArchivos {

	String file;
	HashSet<String> camposRequeridos = new HashSet<String>();

	public void setFile(String file) {
		this.file = file;
	}

	public ArrayList<String> leerTxt() {
		ArrayList<String> aList = new ArrayList<String>();
		File f = new File(file);
		BufferedReader entrada;
		boolean ma = false;
		boolean v = false;

		try {
			entrada = new BufferedReader(new FileReader(f));
			String line;
			while (entrada.ready()) {
				line = entrada.readLine();
				if (line.startsWith("MU") || line.startsWith("C-")
						|| line.startsWith("D-") || line.startsWith("AIR")
						|| line.startsWith("I-") || line.startsWith("K-")
						|| line.startsWith("TAX") || line.startsWith("FM")
						|| line.startsWith("FP") || line.startsWith("FO")
						|| line.startsWith("T-") || line.startsWith("A-")
						|| line.startsWith("H-") || line.startsWith("RM")) {
					if (line.startsWith("AIR")) {
						String[] air;
						air = line.split(";");
						if (air[1].startsWith("MA")) {
							ma = true;
						}
					}
					if (line.startsWith("AMD")) {
						String[] amd;
						amd = line.split(";");

						if (amd[0].indexOf("VOID") > 0) {
							v = true;
						}
					}

					if (line.startsWith("T-") && ma && v) {
						aList.removeAll(aList);
						aList.add(line.substring(7, 17));
						aList.add("CANCELADO");
						break;
					} else {
						aList.add(line);
					}
				}
			}
		} catch (IOException e) {
			e.printStackTrace();
			return null;
		}
		return aList;

	}

	public ArrayList<String> leerDat() {
		ArrayList<String> aList = new ArrayList<String>();
		File f = new File(file);
		BufferedReader entrada;

		try {
			entrada = new BufferedReader(new FileReader(f));
			String line;
			while (entrada.ready()) {
				line = entrada.readLine();
				if (line.startsWith("1\\") || line.startsWith("3\\")
						|| line.startsWith("2") || line.startsWith("6")
						|| line.startsWith("A\\") || line.startsWith("G")
						|| line.startsWith("X") || line.startsWith("8\\")
						|| line.startsWith("7\\") || line.startsWith("C")
						|| line.startsWith("N") || line.startsWith("9\\")
						|| line.startsWith("U\\") || line.startsWith("01")) {
					if (line.startsWith("A\\")) {
						String[] cancel;
						cancel = line.split("\\\\");
						if (cancel[6].startsWith("V")) {
							aList.removeAll(aList);
							aList.add(line.substring(1));
							aList.add("CANCELADO");
							break;
						} else {
							aList.add(line);
						}
					} else {
						aList.add(line);
					}
				}
			}
		} catch (IOException e) {
			e.printStackTrace();
			return null;
		}
		return aList;

	}

	public ArrayList<String> leerPnr() {
		ArrayList<String> aList = new ArrayList<String>();
		File f = new File(file);
		BufferedReader entrada;
		int i = 0;

		try {
			entrada = new BufferedReader(new FileReader(f));
			String line;
			while (entrada.ready()) {
				line = entrada.readLine();
				i++;
				if (line.startsWith("AA") || line.startsWith("M5")
						|| line.startsWith("M2") || i == 16 || i == 8
						|| i == 13 || i == 14 || i == 15
						|| line.startsWith("M8") || line.startsWith("M3")) {
					if (line.startsWith("AA")) {
						if (line.indexOf("M05") > 0) {
							aList.removeAll(aList);
							aList.add(line.substring(29, 39));
							aList.add("CANCELADO");
							break;
						} else {
							aList.add(line);
						}
					} else {
						aList.add(line);
					}
				}
			}
		} catch (IOException e) {
			e.printStackTrace();
			return null;
		}

		return aList;

	}

	public ArrayList<String> leerBca() {
		ArrayList<String> aList = new ArrayList<String>();
		File f = new File(file);
		BufferedReader entrada;

		try {
			entrada = new BufferedReader(new FileReader(f));
			String line;
			while (entrada.ready()) {
				line = entrada.readLine();
				aList.add(line);
			}
		} catch (IOException e) {
			e.printStackTrace();
			return null;
		}
		return aList;

	}
}
